/* eslint-disable no-console */
import axios from 'axios'
import { errorCodeType } from '@/utils/error-code-type'
import { ElMessage, ElLoading } from 'element-plus'
import router from '../router/index'
// 创建axios实例
const service = axios.create({
  // 服务接口请求
  // baseURL: import.meta.env.VITE_APP_BASE_API,
  baseURL: 'https://gateway.t.yundouping.com/',
  // 超时设置
  // timeout: 15000,
  headers: { 'Content-Type': 'application/json;charset=utf-8' },
})
let isTokenExpired = false
let loading: any
// 正在请求的数量
let requestCount = 0
// 显示loading
const showLoading = () => {
  if (requestCount === 0 && !loading) {
    // 加载中显示样式可以自行修改
    loading = ElLoading.service({
      text: '拼命加载中，请稍后...',
      background: '#fff',
      spinner: 'el-icon-loading',
    })
  }
  requestCount++
}
// 隐藏loading
const hideLoading = () => {
  requestCount--
  // eslint-disable-next-line eqeqeq
  if (requestCount == 0) {
    loading.close()
  }
}

// 请求拦截
service.interceptors.request.use(
  (config) => {
    showLoading()
    // 是否需要设置 token放在请求头
    // config.headers['token'] =  getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    // 在发送请求之前做些什么 token
    if (localStorage.getItem('token')) {
      config.headers!['token'] = localStorage.getItem('token')
    }
    else {
      router.push('/login')
      return config
    }
    // get请求映射params参数 
    if (config.method === 'get' && config.params) {
      let url = config.url + '?'
      for (const propName of Object.keys(config.params)) {
        const value = config.params[propName]
        const part = encodeURIComponent(propName) + '='
        if (value !== null && typeof value !== undefined) {
          // 对象处理
          if (typeof value === 'object') {
            for (const key of Object.keys(value)) {
              const params = propName + '[' + key + ']'
              const subPart = encodeURIComponent(params) + '='
              url += subPart + encodeURIComponent(value[key]) + '&'
            }
          }
          else {
            url += part + encodeURIComponent(value) + '&'
          }
        }
      }
      url = url.slice(0, -1)
      config.params = {}
      config.url = url
    }
    return config
  },
  (error) => {
    Promise.reject(error)
  }
)
// 响应拦截器
service.interceptors.response.use(
  (res: any) => {
    hideLoading()
    // 未设置状态码则默认成功状态
    const code = res.data['code'] || 1
    // 获取错误信息
    const msg = errorCodeType(code) || res.code['msg']
    if (code === 1) {
      return Promise.resolve(res)
    } else if (code == '10010') {

      if (!isTokenExpired) {
        isTokenExpired = true
        if(localStorage.getItem('token')){
          ElMessage.error(msg)
        }
      }
      localStorage.removeItem('token')
      router.push('/login');
    }
    // ElMessage.error(msg)
    return Promise.reject(res)
  },
  (error) => {
    hideLoading()
    let { message } = error
    // eslint-disable-next-line eqeqeq
    if (message == 'Network Error') {
      message = '后端接口连接异常'
    }
    else if (message.includes('timeout')) {
      message = '系统接口请求超时'
    }
    else if (message.includes('Request failed with status code')) {
      message = '系统接口' + message.substr(message.length - 3) + '异常'
    }
    ElMessage.error({
      message: message,
      duration: 5 * 1000,
    })
    return Promise.reject(error)
  }
)

export default service
